<?php
namespace Application\Component;
use Doctrine\ORM\EntityManager;
use Application\Entity\FriendFacebook;
use DateTime;
use Application\Component\AbstractComponent;
/**
 * FriendFacebookServcie
 *
 */
class FriendFacebookComponent extends AbstractComponent
{
    protected  $entity="Application\\Entity\\FriendFacebook";

 
	function insert($friendFacebook)
	{
		$datetime = new DateTime("now");
		$friendFacebook->createdAt = $datetime;
		$friendFacebook->modifiedAt = $datetime;
		$this->em->persist($friendFacebook);
		$this->em->flush();
	}
	
	
	function update($friendFacebook)
	{
		$datetime = new DateTime("now");
		$friendFacebook->modifiedAt = $datetime;
		$this->em->persist($friendFacebook);
		$this->em->flush();
	}
	
	function get($id)
	{
		$friendFacebook = $this->em->find($this->entity, $id);
		return  $friendFacebook;
	}
	
	function getAll()
	{
		$friendFacebook = $this->em->getRepository($this->entity)->findAll();
		return  $friendFacebook;
	}
	
  function getMapFriendFacebook($userFacebookId)
	{
	    $mapFriendFacebook = array();
	 
	    $friendFacebooks = $this->em->getRepository($this->entity)->getByUserFacebook($userFacebookId);
	    foreach($friendFacebooks[0] as $friendFacebook)
	    {
	    	if($friendFacebook->userTo==$userFacebookId)
	    	{
	    		if(!array_key_exists($friendFacebook->userFrom,$mapFriendFacebook))
	    		{
	    		    $mapFriendFacebook[$friendFacebook->userFrom] = $friendFacebook;
	    		}
	    	}else if($friendFacebook->userFrom==$userFacebookId){
	    	    if(!array_key_exists($friendFacebook->userTo,$mapFriendFacebook))
	    	    {
	    	    	$mapFriendFacebook[$friendFacebook->userTo] = $friendFacebook;
	    	    }
	    	}
	    }
	    return  $mapFriendFacebook;
	}
	function getByUserFacebook($userFacebookId)
	{
		$friendFacebook = $this->em->getRepository($this->entity)->getByUserFacebook($userFacebookId);
		return  $friendFacebook;
	}
	
	function getByUserFacebookTo($userFacebookId)
	{
		$friendFacebook = $this->em->getRepository($this->entity)->getByUserFacebookTo($userFacebookId);
		return  $friendFacebook;
	}
	function getByUserFacebookFrom($userFacebookId)
	{
		$friendFacebook = $this->em->getRepository($this->entity)->getByUserFacebookFrom($userFacebookId);
		return  $friendFacebook;
	}
	
	
	function delete($id)
	{
		$friendFacebook = $this->em->find($this->entity, $id);
		$this->em->remove($friendFacebook);
		$this->em->flush();
	}
}
